﻿using Max.PH5_2010A.Shopping.Domain;
using Max.PH5_2010A.Shopping.DTO;
using Max.PH5_2010A.Shopping.IRespository;
using Max.PH5_2010A.Shopping.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Max.PH5_2010A.Shopping.Service
{
    public class MemberService : BaseService<MemberModel>, IMemberService
    {
        IMemberRespository _respository;
        IUnitOfWork _unitOfWork;
        /// <summary>
        /// 自己的事情，自己做。会员服务，要用数据，就用自己的仓储数据，而不是BaseRespository
        /// </summary>
        /// <param name="respository"></param>
        public MemberService(IMemberRespository respository, IUnitOfWork unitOfWork) : base(respository, unitOfWork)
        {
            _respository = respository;
            _unitOfWork = unitOfWork;
        }


        /// <summary>
        /// 会员登录
        /// </summary>
        /// <param name="name"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public Tuple<bool, string, MemberOutputDto> Login(string name, string password)
        {
            var data = _respository.Select().Where(x => x.LoginName.Equals(name)).FirstOrDefault();

            if (data == null)
            {
                //说明：数据表中不存在这个用户
                return Tuple.Create(false, "用户不存在", new MemberOutputDto());
            }

            //if (!data.State.Equals("1"))
            //{
            //    return Tuple.Create(false, "用户已被禁用", new MemberOutputDto());
            //}

            password = MD5Hash.Hash.Content(password);

            if (!data.LoginPassword.Equals(password))
            {
                //说明：用户存在，密码错误。
                return Tuple.Create(false, "密码有误", new MemberOutputDto());
            }

            //手动赋值，可以用第三个AutoMapper去做
            MemberOutputDto dto = new()
            {
                CardNo = data.CardNo,
                HeadImg = data.HeadImg,
                Id = data.Id,
                LoginName = data.LoginName,
                Name = name,
                Sex = data.Sex,
            };

            return Tuple.Create(true, "登录成功", dto);
        }

        public Tuple<bool, string> Register(MemberInputDto dto)
        {
            var m = _respository.Select().Where(x => x.LoginName.Equals(dto.LoginName)).FirstOrDefault();
            if (m != null)
            {
                return Tuple.Create(false, "用户名已存在");
            }

            //将dto转为model
            MemberModel model = new MemberModel()
            {
                LoginName = dto.LoginName,
                HeadImg = dto.HeadImg,
                LoginPassword = dto.LoginPassword,
                Name = dto.Name,
                Sex = dto.Sex,
                CardNo = dto.CardNo 
            };


            //密码加密-采用MD5
            //为什么用它？不可逆  -- 暴力破解：穷举
            //1、安装MD5包
            //2、string key = MD5Hash.Hash.Content("123");
            model.LoginPassword = MD5Hash.Hash.Content(model.LoginPassword);

            _respository.Insert(model);
            var result = _unitOfWork.SaveChanges();
            if (result > 0)
            {
                return Tuple.Create(true, "注册成功");
            }
            else
            {
                return Tuple.Create(false, "服务器错误");
            }
        }
    }
}
